﻿<%@ Page Title="" Language="C#" MasterPageFile="BGMasterPage.master" AutoEventWireup="true"
    CodeFile="Sync.aspx.cs" Inherits="Sync" %>

<asp:Content ID="Content1" ContentPlaceHolderID="cphHeader" runat="Server">
    <style type="text/css">
        .StatusPanel div
        {
            color: Green;
            font-weight: bold;
        }
        .ProgressBar
        {
            margin-top: 2px;
        }
        .Message
        {
            height: 30px;
            line-height: 30px;
            margin-right: 5px;
        }
    </style>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="cphContent" runat="Server">
    <input type="button" id="syncButton" onclick="sync()" value="同步" class="ui-button ui-submit" />
    <input type="button" id="downloadButton" onclick="download()" class="ui-button ui-submit" value="下载图片" />
    <br />
    <asp:Panel ID="Panel1" runat="server" GroupingText="同步结果" CssClass="StatusPanel">
        <div id="syncMessage" class="Message">
        </div>
        <div id="progressBar" class="ProgressBar">
        </div>
    </asp:Panel>
    <asp:Panel ID="Panel2" runat="server" GroupingText="下载结果" CssClass="StatusPanel">
        <div id="downloadMessage" class="Message">
        </div>
        <div id="downloadProgressBar" class="ProgressBar">
        </div>
    </asp:Panel>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="Bottom" runat="Server">
    <script type="text/javascript">
        $(function () {
            //$("#progressBar").progressbar({ value: false })
        });
        var timer = null;
        function sync() {
            $("#syncButton").disable();
            $.ajax({
                url: "handler/SyncHandler.ashx?" + new Date(), data: "mode=Sync", type: "post",
                success: function (response) {
                    var data = $.parseJSON(response);
                    if (data.Result || data.Message == "运行中") {
                        $("#syncMessage").text("正在同步...");
                        timer = window.setInterval("querySyncStatus()", 1000);
                    } else {
                        $("#syncMessage").text("开始同步时出错：" + data.Message);
                        $("#syncButton").enable();
                    }
                },
                error: function () {
                    $("#syncMessage").text("开始同步时出错!");
                    $("#syncButton").enable();
                }
            });
        }
        var downloadTimer = null;
        function download() {
            $("#downloadButton").disable();
            $.ajax({
                url: "handler/SyncHandler.ashx?" + new Date(), data: "mode=DownloadImg", type: "post",
                success: function (response) {
                    var data = $.parseJSON(response);
                    if (data.Result || data.Message == "运行中") {
                        $("#downloadMessage").text("正在下载...");
                        downloadTimer = window.setInterval("queryDownloadStatus()", 1000);
                    } else {
                        $("#downloadMessage").text("开始下载时出错：" + data.Message);
                        $("#downloadButton").enable();
                    }
                },
                error: function () {
                    $("#downloadMessage").text("开始下载时出错!");
                    $("#downloadButton").enable();
                }
            });
        }
        function querySyncStatus() {
            if (timer == null) { return; }
            $.ajax({
                url: "handler/SyncHandler.ashx?mode=GetSyncStatus&" + new Date(), type: "get",
                success: function (response) {
                    if (timer == null) { return; }
                    var data = $.parseJSON(response);
                    if (data.Result) {
                        $("#syncMessage").text("正在同步..." + data.CurrentIndex + "/" + data.TotalCount);
                        console.debug(data.CurrentIndex + ":" + data.TotalCount + ":" + rate);
                        if (data.CurrentIndex == 0) { return; }
                        var rate = (data.CurrentIndex * 100 / data.TotalCount);
                        $("#progressBar").progressbar({ value: rate })
                        if (rate == 100) {
                            timer = null; window.clearInterval(timer);
                            $("#syncMessage").html("同步完成!");
                            $("#syncButton").enable();
                            if (data.Message) {
                                $.jqAlert(data.Message);
                            }
                        }
                    } else {
                        //$("#syncMessage").html("正在同步...<span style='color:red'>无法获取进度</span>");
                    }
                },
                error: function () {

                }
            });
        }
        function queryDownloadStatus() {
            if (downloadTimer == null) { return; }
            $.ajax({
                url: "handler/SyncHandler.ashx?mode=GetDownloadImgStataus&" + new Date(), type: "get",
                success: function (response) {
                    if (downloadTimer == null) { return; }
                    var data = $.parseJSON(response);
                    if (data.Result) {
                        $("#downloadMessage").text("正在下载..." + data.CurrentIndex + "/" + data.TotalCount);
                        if (data.CurrentIndex == 0) { return; }
                        var rate = (data.CurrentIndex * 100 / data.TotalCount);
                        $("#downloadProgressBar").progressbar({ value: rate })
                        if (rate == 100) {
                            downloadTimer = null;
                            window.clearInterval(downloadTimer);
                            $("#downloadMessage").html("下载完成!");
                            $("#downloadButton").enable();
                            if (data.Message) {
                                $.jqAlert(data.Message);
                            }
                        }
                    } else {
                        //$("#downloadMessage").html("正在下载...<span style='color:red'>无法获取进度</span>");
                    }
                },
                error: function () {

                }
            });
        }
    </script>
</asp:Content>
